package cn.wzy.config;


import cn.wzy.interceptor.LoginInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.annotation.Resource;

@Configuration
@EnableSwagger2
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
  @Resource
  private LoginInterceptor loginInterceptor;


  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("doc.html")
      .addResourceLocations("classpath:/META-INF/resources/");

    registry.addResourceHandler("swagger-ui.html")
      .addResourceLocations("classpath:/META-INF/resources/");
    registry.addResourceHandler("/webjars/**")
      .addResourceLocations("classpath:/META-INF/resources/webjars/");


    registry.addResourceHandler("/**")
      .addResourceLocations("classpath:/static/")
      .addResourceLocations("classpath:/META-INF/resources/");
  }
  @Override
  public void addCorsMappings(CorsRegistry registry) {
    // 设置允许跨域的路径
    registry.addMapping("/**")
      // 设置允许跨域请求的域名
      .allowedOriginPatterns("*")
      // 是否允许cookie
      .allowCredentials(true)
      // 设置允许的请求方式
      .allowedMethods("GET", "POST", "DELETE", "PUT","PATCH")
      // 设置允许的header属性
      .allowedHeaders("*")
      // 跨域允许时间
      .maxAge(3600);
  }

  @Override
  public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(loginInterceptor)
            .addPathPatterns("/**")
            .excludePathPatterns("/login","loginOut")
            .excludePathPatterns("/css/**","/fonts/**","/images/**","/js/**");
  }

}
